package com.mysteel.monitor.controller;


import com.mysteel.monitor.core.annotation.Monitor;
import com.mysteel.monitor.core.annotation.MonitorMethod;
import com.mysteel.monitor.core.component.CPUMonitor;
import com.mysteel.monitor.core.component.SystemComponent;
import com.mysteel.monitor.core.model.SystemInfo;
import com.mysteel.monitor.core.response.RespResult;
import com.mysteel.monitor.core.utils.ThreadRenderUitl;
import com.mysteel.monitor.core.utils.ThreadUtil;


import java.util.Map;

/**
 * @author Administrator
 */
@Monitor("/v1/api/monitor")
public class MonitorController {

    private SystemComponent systemComponent = new SystemComponent();

    /**
     * 基本信息
     * @return
     */
    @MonitorMethod("info")
    public RespResult<String> info(){
        // 获取初始话信息
        return RespResult.success(systemComponent.ipAndhostName());
    }

    /**
     * 基本信息
     * @return
     */
    @MonitorMethod("baseinfo")
    public RespResult<SystemInfo> baseinfo(){
        // 获取初始话信息
      return RespResult.success(systemComponent.initSystemInfo());
    }

    /**
     * 线程信息
     * @return
     */
    @MonitorMethod("thread")
    public RespResult threadinfo(){
        Map<String, Thread> threads = ThreadUtil.getThreads();
        // 获取初始话信息
        return RespResult.success(ThreadRenderUitl.renderer(threads.values().iterator()));
    }



    /**
     * 内存信息
     * @return
     */
    @MonitorMethod("memory")
    public RespResult memoryinfo(){
        // 获取初始话信息
        return RespResult.success(systemComponent.memoryUsageInfos());
    }

    /**
     * GC信息
     * @return
     */
    @MonitorMethod("gc")
    public RespResult gcinfo(){
        // 获取初始话信息
        return RespResult.success(systemComponent.gcInfoList());
    }

    /**
     *  CPU 信息
     * @return
     */
    @MonitorMethod("cpu")
    public RespResult cpuinfo(){
        // CPU使用率
        return RespResult.success(CPUMonitor.getInstance().getProcessCpu());
    }

}
